package com.wedistudio.android.framework.html;

import java.io.EOFException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTMLTools {
	public static boolean exists(URL url) {
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("Accept-Encoding",
					"identity");
			connection.setRequestMethod("HEAD");
			int code = connection.getResponseCode();
			return (code == 200);
			// You can determine on HTTP return code received. 200 is success.
		} catch (EOFException e) {
			return true;
		} catch (IOException e) {
			return false;
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
	}

	public static long size(URL url) {
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("HEAD");
			return connection.getContentLength();
		} catch (IOException e) {
			return -1;
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
	}
}
